 #!/usr/bin/python
 # -*- coding: utf-8 -*-

import json
import os, sys
reload(sys)
sys.setdefaultencoding('utf-8')
import traceback
import time,datetime

def format_user_time(time_str):
    try:
        t_str = time_str.split(" ") #October 3rd, 2016
        t_m = t_str[0].strip(".")
        t_d = t_str[1].strip(",").strip("st|nd|rd|th")
        t_y = t_str[2]
        month_num={"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sept":9,"Oct":10,"Nov":11,"Dec":12,"January":1,"February":2,"March":3,"April":4,"June":6,"July":7,"August":8,"September":9,"October":10,"November":11,"December":12}
        t_m = str(month_num[t_m])
        return (t_d,t_m,t_y)
    except:
        traceback.print_exc()

def get_reviweCntPerMonth(user_review_fin, reviewer_name):

    uhead="reviewer, review_id, submitter, review_time"
    errhead="reviewer, review_id, submitter, summary, review_time"
    print>>file("formatted_user_reviews","w"), uhead
    print>>file("err_format_user","w"), errhead
    u_m_review = {}
    for u in user_review_fin.xreadlines():
        try:
            #: /r/50155/#review151139邹maugli邹SQOOP-2983: OraOop export has degraded performance with wide tables邹October 3rd, 2016
            rid, submitter, summary, review_time  = u.strip().split("邹")
            (review_d,review_m,review_y) = format_user_time(review_time)

            formatted_reviewtime="-".join([review_d, review_m, review_y])
            print>> file("formatted_user_reviews","a"), ",".join([reviewer_name, rid, submitter, formatted_reviewtime])

            k = (reviewer_name,"-".join([review_m,review_y]))
            if k not in u_m_review:
                u_m_review[k] = 0
            u_m_review[k] += 1

        except:
            print>> file("err_user_review",'a'), u.strip()
            traceback.print_exc()
    for (u,m) in u_m_review.keys():
        print ",".join([u, m, str(u_m_review[(u,m)])])

#python reviewCntPerMonth_user.py davidzchen_reviews.txt davidezchen
if __name__ == '__main__':
    user_review_fin = sys.argv[1]
    reviewer_name = sys.argv[2]
    get_reviweCntPerMonth(file(user_review_fin,'r'), reviewer_name)
